<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入店铺名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营区域" prop="operatingArea">
        <el-input
            v-model="queryParams.operatingArea"
            placeholder="请输入经营区域"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:merchant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:merchant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:merchant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:merchant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column label="排名" align="center" width="80">
        <template #default="scope">
          <div class="ranking-item">
            {{ scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="店铺名" align="center" prop="name" />
      <el-table-column label="经营区域" align="center" prop="operatingArea" />
      <el-table-column label="经营人" align="center" prop="director" >
        <template #default="scope">
          <div>{{ scope.row.director }}</div>
          <div>{{ scope.row.mobile }}</div>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="商家评分" align="center" prop="rate" >
        <template #default="scope">
          <table>
          <div v-if="scope.row.rate===5">
            <div style="color: gold;font-size: 30px">★★★★★</div>
            <div>五星</div>
          </div>
          <div v-if="scope.row.rate===4">
            <div  style="color: gold;font-size: 30px">★★★★</div>
            <div>四星</div>
          </div>
          <div v-if="scope.row.rate===3">
            <div  style="color: gold;font-size: 30px">★★★</div>
            <div>三星</div>
          </div>
          <div v-if="scope.row.rate===2">
            <div  style="color: gold;font-size: 30px">★★</div>
            <div>二星</div>
          </div>
          <div v-if="scope.row.rate===1">
            <div  style="color: gold;font-size: 30px">★</div>
            <div>一星</div>
          </div>
          </table>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Merchant">
import { listMerchantbyrate, getMerchant, delMerchant, addMerchant, updateMerchant } from "../../../api/system/merchant.js"
import {listVerification} from "../../../api/system/verification.js"

const { proxy } = getCurrentInstance()

const merchantList = ref([])
const open = ref(false)
const form1=ref({})
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const opendetail = ref(false)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    operatingArea: null,
    regTime: null,
    startTime: null,
    endTime: null,
    address: null,
    longitude: null,
    latitude: null,
    description: null,
    rate: null,
    status: null,
    deleted: null,
    auditStatusId: null,
    currentBalance: null
  },
  rules: {
    name: [
      { required: true, message: "店铺名不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商户列表 */
function getList() {
  loading.value = true
  listMerchantbyrate(queryParams.value).then(response => {
    console.log(response)
    let a=[]
    let b=[]
    total.value = response.total
    loading.value = false
    a=response.rows
    listVerification().then(response => {
      b=response.rows
      for (let i=0;i<a.length;i++){
        for (let j=0;j<b.length;j++){
          if(a[i].id===b[j].mid){
            a[i].director=b[j].director
            a[i].mobile=b[j].mobile
            a[i].idCard=b[j].idCard
            a[i].businessLicense=b[j].businessLicenseId
          }
        }
      }
      merchantList.value= a
    })
  })
}
//展示详情
function handleDetail(row) {
  getMerchant(row.id).then(response => {
    opendetail.value=true
    form1.value = row
    console.log(form1.value)
  })
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    operatingArea: null,
    regTime: null,
    startTime: null,
    endTime: null,
    address: null,
    longitude: null,
    latitude: null,
    description: null,
    rate: null,
    status: null,
    deleted: null,
    auditStatusId: null,
    currentBalance: null,
    businessLicenseId: null,
    director: null,
    idCard: null,
    mobile: null,
  }
  proxy.resetForm("merchantRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商户"
}
//审核通过
function pass(){
  form1.value.auditStatusId = 1
  updateMerchant(form1.value).then(response=>{
    console.log(form1.value)
    opendetail.value = false
  })
}
//审核不通过
function out(){
  form1.value.auditStatusId = 2
  updateMerchant(form1.value).then(response=>{
    opendetail.value = false
  })
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMerchant(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商户"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMerchant(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除商户编号为"' + _ids + '"的数据项？').then(function() {
    return delMerchant(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/merchant/export', {
    ...queryParams.value
  }, `merchant_${new Date().getTime()}.xlsx`)
}

getList()
// onMounted(() => {
//   getList()
// })
</script>
<style scoped lang="scss">
.merchant-info{
  width: 90%;
  padding-left: 5%;
  padding-right: 5%;
  margin-top: 50px;
}
h2 {
  margin-bottom: 10px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border: 1px solid #000;
  padding: 8px;
  text-align: center;
  height: 80px;
  width: 160px;
}

th {
  background-color: #f2f2f2;
}

.photo-group {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.photo-group img {
  width: 150px;
  height: auto;
}
</style>
